﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using OT.Data;
using OT.Areas.Student.Models;
namespace OT.Areas.Student.Controllers
{
    public class HomeController : Controller
    {
        #region Declaration
        public static List<StudentAttemptTestCounter_Result> dataobj2;
        public static List<BarDataStudent_Result> data1obj;
        readonly OTEntitiesme OTDb = new OTEntitiesme();
        HttpCookie myCookie1 = new HttpCookie("LoginSes");
        HttpCookie myCookie2 = new HttpCookie("LoginSesID");
        HttpCookie myCookie3 = new HttpCookie("email");
        #endregion

        #region Isvarifyjsoncall
        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult Isvarify(string dt)
        {
            int UserID = 0;
            int cnt = 0;
            if (Request.Cookies["LoginSesID"] != null)
            {
                UserID = Convert.ToInt16(Request.Cookies["LoginSesID"].Value);
               cnt = OTDb.Users.Where(p => p.UserID.Equals(UserID) && p.IsVarify == false).Count();
               
            }
            return Json(cnt, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region ActivationMail
        public ActionResult ActivationMail(string dt)
        {
            try
            {
                string Emailreceiver =Request.Cookies["email"].ToString() ;
                string activationCode = OTDb.Users.Where(u => u.EmailAddress.Trim().Equals(Emailreceiver.Trim())).FirstOrDefault().Guid.Value.ToString();
                string b2 = Request.Url.Authority;
                string b6 = "http://" + b2 + "/Home/SuccessReg?ActivationCode=" + activationCode;
                string body = "Hello " + Request.Cookies["LoginSes"].ToString() + ",";
                body += "<br /><br />Please click the following link to activate your account";
                body += "<br /><a href = '" + b6 + "'>Click here to activate your account.</a>";
                body += "<br /><br />Thanks";
                Utilities.SendEmail(Emailreceiver, "Activation Link for Registration Process", body);
            }
            catch (Exception e)
            {
                Utilities.InsertErrorLog(e);
            }

            return View("index");
        }
        #endregion

        #region Index
        public ActionResult Index()
        {
            try
            {
                if (Request.QueryString["Register"] == "sc")
                {
                    Session["sc"] = "Successfuly Registered";
                }
                else
                {
                    Session["sc"] = null;
                }
                if (Request.QueryString["lg"] == "lg")
                {
                    myCookie1.Expires = DateTime.Now.AddDays(-1d);
                    Response.Cookies.Add(myCookie1);
                    myCookie2.Expires = DateTime.Now.AddDays(-1d);
                    Response.Cookies.Add(myCookie2);
                    myCookie3.Expires = DateTime.Now.AddDays(-1d);
                    Response.Cookies.Add(myCookie3);
                    Response.Redirect("~/home", false);
                }
                else
                {
                    int UserID = 0;
                    if (Request.Cookies["LoginSesID"] != null)
                    {
                        UserID =Convert.ToInt16(Request.Cookies["LoginSesID"].Value);
                    }
                    else
                    {
                        TempData["notice"] = "Time Out ,Please Login";
                        Response.Redirect("~/Home");
                    }
                    if (ModelState.IsValid)
                    {
                        var User = OTDb.Users.Where(r => r.UserID == UserID).SingleOrDefault();
                        if (User != null)
                        {
                            User.LastLoginDate = DateTime.Now;
                            OTDb.SaveChanges();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return View();
        }
#endregion    

        #region Logout
        public int Logout()
        {
            FormsAuthentication.SignOut();
            myCookie1.Expires = DateTime.Now.AddDays(-1d);
            Response.Cookies.Add(myCookie1);

            myCookie2.Expires = DateTime.Now.AddDays(-1d);
            Response.Cookies.Add(myCookie2);
            Response.Redirect("~/Student", false);
            return 0;
        }
        #endregion

        #region jsoncallforBarData
        public JsonResult BarDataTest()
        {
            int UserID=0;
            if (Request.Cookies["LoginSesID"] != null)
            {
                UserID = Convert.ToInt16(Request.Cookies["LoginSesID"].Value);
            }
            return Json(new
            {
                labels = OTDb.BarDataStudent(UserID).Select(p => p.TestName).ToList(),
                datasets = new 
               {
                    data = OTDb.BarDataStudent(UserID).Select(p => Math.Round((double)p.Average,2)).ToList(),
                    fillcolor = "rgba(151,187,205,0.3)",
                    strokeColor = "rgba(151,187,205,0.5)",
                    highlightFill = "rgba(151,187,205,0.9)",
                    highlightStroke = "rgba(151,187,205,1)"
               } 
                
            });
        }
       
        public JsonResult BarDataAttempt()
        {
            int UserID = 0;
            if (Request.Cookies["LoginSesID"] != null)
            {
                UserID = Convert.ToInt16(Request.Cookies["LoginSesID"].Value);
            }
            dataobj2=OTDb.StudentAttemptTestCounter(UserID).ToList<StudentAttemptTestCounter_Result>();
            return Json(new
            {
                dataobj2
            });
        }
        #endregion
    }
}
